import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/uploadServlet")
@MultipartConfig(location = "C:\\Users\\user\\Desktop\\third_comprehensive_experiment\\src\\main\\webapp\\images")
public class uploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession(true);
        Part part = request.getPart("file");
        String fileName = part.getSubmittedFileName();
        System.out.println("上传文件的文件名: " + fileName);
        if(fileName==null || fileName.equals(""))
            response.sendRedirect("/error.html");
        session.setAttribute("path",fileName);
        part.write(fileName);
        response.sendRedirect("/user.jsp");
    }
}
